from fastapi import APIRouter, UploadFile, File, HTTPException, Depends
from pydantic import BaseModel
from typing import Optional, Dict
from utils.file_handler import save_temp_file, remove_temp_file
from utils.logger import setup_logger
from models.dependencies import get_dependencies

logger = setup_logger()

# 假设ModelHandler和ModelConfig定义在你的代码中
from data_handler.data_model_process import ModelHandler
from config import ModelConfig ,MODEL_CONFIG # 替换为实际模块路径
# ChatTTS 流式路由
router_chattts = APIRouter(prefix="/chattts", tags=["ChatTTS"])

class ChatTTSConfig(BaseModel):
    text: str
    speaker: str = MODEL_CONFIG.chattts_speaker
    speed: float = 1.0
    temperature: float = 0.7
    stream: bool = True

@router_chattts.post("/stream/generate")
async def chattts_stream_generate(
    config: ChatTTSConfig,
    dependencies: Dict = Depends(get_dependencies)
):
    try:
        handler = ModelHandler()
        model_config = ModelConfig(
            model="chattts",
            temperature=config.temperature,
            max_tokens=512,
            stream=True,
            text=config.text,
            speaker=config.speaker,
            speed=config.speed,
            chattts_model_dir=MODEL_CONFIG.chattts_model_dir
        )
        result = await handler.call_model(
            model="chattts",
            model_type="chattts",
            data=config.text,
            streaming=True,
            config=model_config,
            dependencies=dependencies
        )
        return result  # StreamingResponse
    except Exception as e:
        logger.error(f"ChatTTS 流式处理失败: {str(e)}")
        raise HTTPException(status_code=500, detail=f"流式处理失败: {str(e)}")
